package org.simple.framework.html.parser.convert.converter;

import org.simple.framework.html.parser.convert.TypeConverter;
import org.simple.framework.html.parser.convert.exception.UnsupportedConvertDataException;

/**
 * 项目名称：simple-framework
 * 类名称：BooleanConverter
 * 类描述：BooleanConverter
 * 创建时间：2018/9/25
 *
 * @author sisyphus   (E-mail:1620657419@qq.com)
 * @version v1.0
 */
public class BooleanConverter implements TypeConverter {

    @Override
    public boolean isSupported(Class clazz) {
        return Boolean.class.isAssignableFrom(clazz) || boolean.class.isAssignableFrom(clazz);
    }

    @Override
    public Boolean convert(Object o, Class clazz) {
        if (o == null) {
            return null;
        }
        if (o instanceof Boolean) {
            return (Boolean) o;
        }
        if (o instanceof String) {
            return Boolean.valueOf((String) o);
        }
        throw new UnsupportedConvertDataException();
    }

}
